package com.mcxx.modules.common.service.impl;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.common.service.StatisticService;
import com.mcxx.modules.statistics.dto.StatisticDTO;
import com.mcxx.modules.statistics.dto.SubApprovalDTO;
import com.mcxx.modules.statistics.entity.SubApprovalBuFaEntity;
import com.mcxx.modules.statistics.param.StatisticParam;
import com.mcxx.modules.statistics.service.read.StatisticReadService;
import com.mcxx.modules.system.entity.AreaSalvationEntity;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Slf4j
@Service
@TaTransactional
public class StatisticSubSummaryServiceImpl extends StatisticService {
    @Autowired
    private StatisticReadService statisticReadService;

    @Override
    public List<? extends StatisticDTO> getStatistic(StatisticParam param, UserAccountVo accountVo, String standType) {
        List<TaAreaPo> taAreaPos = new ArrayList<TaAreaPo>();
        String name = super.commonStatisticStart(param, accountVo, taAreaPos, null);
        if(taAreaPos.isEmpty()){
            throw new BizException(FailureCode.ERR_100002);
        }

        TaAreaPo curTaAreaPo = taAreaPos.get(0);

        Integer level = curTaAreaPo.getAreaLevel();
        if (curTaAreaPo.getAreaLevel() < Constant.AreaLevel.COMMUNITY)
            level += 1;

        String validCode = areaUtil.getValidCode(curTaAreaPo.getAreaCode(), curTaAreaPo.getAreaLevel());
        Integer validCodeLen = areaUtil.getValidCodeLen(curTaAreaPo.getAreaLevel() + 1);
        String zeros =   String.join("", Collections.nCopies(12 - validCodeLen, "0"));

        String formatDate = param.getMonth();
        formatDate = formatDate.substring(0, 4) + "-" + formatDate.substring(4) + "-01";

        List<SubApprovalDTO> results = null;
        if(standType.equals(Constant.BusinessStandType.CITY_DIBAO)){
            name += "城市低保审批汇总表";
            results = statisticReadService.getCitySubSummary(validCode, level, validCodeLen, zeros, formatDate);
        }
        else{
            name += "农村低保审批汇总报表";
            results = statisticReadService.getCountrySubSummary(validCode, level, validCodeLen, zeros, formatDate);
        }
        List<SubApprovalBuFaEntity> bufas = statisticReadService.getSubSummaryBuFa(validCode, level,
                validCodeLen, zeros, formatDate, standType);

        for(SubApprovalDTO dto : results){
            dto.setTitle(name);
            for(SubApprovalBuFaEntity bufa : bufas){
                if(bufa.getAreaCode().equals(dto.getAreaCode())){
                    dto.setTiaobiao(bufa.getTiaobiao());
                    dto.setZidingyi(bufa.getZidingyi());
                    break;
                }
            }
        }
        return results;
    }

    @Override
    public void fillSalvation(StatisticDTO dto, List<String> standards, List<AreaSalvationEntity> salivations) {

    }
}
